package com.gitee.zycra.union.api.web.controller;

import com.gitee.zycra.jdbc.model.PageResult;
import com.gitee.zycra.union.api.common.bo.CommonResult;
import com.gitee.zycra.union.api.common.bo.PageQuery;
import com.gitee.zycra.union.api.common.param.AddMockInstanceParam;
import com.gitee.zycra.union.api.common.param.PageQueryMockTemplateParam;
import com.gitee.zycra.union.api.common.vo.MockInterfaceVO;
import com.gitee.zycra.union.api.common.vo.MockTemplateInterfaceVO;
import com.gitee.zycra.union.api.common.vo.MockTemplateVO;
import com.gitee.zycra.union.api.service.ApiMockInstanceService;
import com.gitee.zycra.union.api.service.ApiMockTemplateService;
import com.gitee.zycra.union.api.web.annotation.AuthCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zycra
 */
@RestController
@RequestMapping("/{groupId}/mock")
public class ApiMockController {

    @Autowired
    private ApiMockTemplateService apiMockTemplateService;

    @Autowired
    private ApiMockInstanceService apiMockInstanceService;

    /**
     * 添加mock模板
     *
     * @param groupId      项目组ID
     * @param templateName 模板名称
     * @return 添加结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/addMockTemplate")
    public CommonResult<String> addMockTemplate(@PathVariable("groupId") Long groupId, @RequestParam("templateName") String templateName) {
        return apiMockTemplateService.addMockTemplate(groupId, templateName);
    }

    /**
     * 更新mock模板
     *
     * @param id           模板ID
     * @param templateName 模板名称
     * @return 添加结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/updateMockTemplate")
    public CommonResult<String> updateMockTemplate(@RequestParam("id") Long id, @RequestParam("templateName") String templateName) {
        return apiMockTemplateService.updateMockTemplate(id, templateName);
    }

    /**
     * 删除mock模板
     *
     * @param id 模板ID
     * @return 添加结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/deleteMockTemplate")
    public CommonResult<String> deleteMockTemplate(@RequestParam("id") Long id) {
        return apiMockTemplateService.deleteMockTemplate(id);
    }

    /**
     * 查询mock模板
     *
     * @param pageQuery 查询参数
     * @return 添加结果
     */
    @AuthCheck
    @PostMapping("/mockTemplateList")
    public CommonResult<PageResult<MockTemplateVO>> mockTemplateList(@RequestBody @Validated PageQuery<PageQueryMockTemplateParam> pageQuery) {
        return apiMockTemplateService.selectMockTemplateForPage(pageQuery);
    }

    /**
     * 添加mock模板接口
     *
     * @param templateId  模板ID
     * @param interfaceId 接口ID
     * @return 添加结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/addMockTemplateInterface")
    public CommonResult<String> addMockTemplateInterface(@RequestParam("templateId") Long templateId, @RequestParam("interfaceId") Long interfaceId) {
        return apiMockTemplateService.addMockTemplateInterface(templateId, interfaceId);
    }

    /**
     * 删除mock模板接口
     *
     * @param id 模板接口ID
     * @return 删除结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/deleteMockTemplateInterface")
    public CommonResult<String> deleteMockTemplateInterface(@RequestParam("id") Long id) {
        return apiMockTemplateService.deleteMockTemplateInterface(id);
    }

    /**
     * 查询mock模板接口
     *
     * @param templateId 模板ID
     * @return 查询结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/mockTemplateInterfaceList")
    public CommonResult<List<MockTemplateInterfaceVO>> mockTemplateInterfaceList(@RequestParam("templateId") Long templateId) {
        return apiMockTemplateService.selectMockTemplateInterfaceByTemplateId(templateId);
    }

    /**
     * 查询mock模板接口和期望信息
     *
     * @param templateId 模板ID
     * @return 查询结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/mockTemplateInterfaceAndCaseList")
    public CommonResult<List<MockInterfaceVO>> mockTemplateInterfaceAndCaseList(@RequestParam("templateId") Long templateId) {
        return apiMockTemplateService.selectByTemplateIdForAdd(templateId);
    }

    /**
     * 添加mock实例
     *
     * @param addMockInstanceParam  添加参数
     * @return 添加结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/addMockInstance")
    public CommonResult<String> addMockInstance(@RequestBody @Validated AddMockInstanceParam addMockInstanceParam) {
        return apiMockInstanceService.addMockInstance(addMockInstanceParam);
    }

    /**
     * 更新mock模板实例
     *
     * @param id           模板实例ID
     * @param instanceName 模板实例名称
     * @return 更新结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/updateMockInstance")
    public CommonResult<String> updateMockInstance(@RequestParam("id") Long id, @RequestParam("instanceName") String instanceName) {
        return apiMockInstanceService.updateMockInstance(id, instanceName);
    }
}
